package com.taoyibao.hongcanting.ui.mine.activity;

import android.text.TextUtils;
import android.view.View;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.activity.BaseActivityPresenter;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.constant.NetCode;
import com.taoyibao.hongcanting.dialog.DialogComment;
import com.taoyibao.hongcanting.dialog.DialogInoutPayPwd;
import com.taoyibao.hongcanting.http.DefaultTransformer;
import com.taoyibao.hongcanting.http.JsonApiWrapper;
import com.taoyibao.hongcanting.http.ProgressSubscriber;
import com.taoyibao.hongcanting.model.BalanceModel;
import com.taoyibao.hongcanting.model.CompanyBaseModel;
import com.taoyibao.hongcanting.model.UserInfoModel;
import com.taoyibao.hongcanting.ui.mine.delegate.WithdrawDelegate;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.taoyibao.hongcanting.utils.DataUtils;
import com.taoyibao.hongcanting.utils.ToastUtils;

import java.util.List;

/**
 * @author TangAnna
 * @description: 提现
 * @date :${DATA} 15:28
 */
public class WithdrawActivity extends BaseActivityPresenter<WithdrawDelegate> implements View.OnClickListener {
    private double mBalance = -1;

    @Override
    protected Class<WithdrawDelegate> getDelegateClass() {
        return WithdrawDelegate.class;
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        viewDelegate.get(R.id.btn_withdraw_confirm).setOnClickListener(this);
        getUserBalance();
    }

    /**
     * 获取用户余额
     */
    public void getUserBalance() {
        JsonApiWrapper.getApiService().userBalance(SpUtils.getToken(), SpUtils.getMemberId())
                .compose(new DefaultTransformer<CompanyBaseModel<List<BalanceModel>>>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel<List<BalanceModel>>>(this, false) {
                    @Override
                    public void onNext(CompanyBaseModel<List<BalanceModel>> balanceModel) {
                        if (balanceModel.code == NetCode.HTTP_200 && balanceModel.data != null && balanceModel.data.size() > 0) {
                            BalanceModel data = balanceModel.data.get(0);
                            mBalance = data.balance;
                        }else {
                            super.onNext(balanceModel);
                        }
                    }
                });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_withdraw_confirm:
                if (TextUtils.equals(viewDelegate.getButtonText(), CodeUtils.getIdString(R.string.string_complete))) {//申请成功之后
                    finish();
                } else {
                    if (!TextUtils.isEmpty(viewDelegate.getAccount()) && viewDelegate.getAmount() != 0) {
                        if (mBalance == -1) {//获取账户余额失败
                            isSetPayPwd();
                        } else if (viewDelegate.getAmount() > mBalance) {//提现金额大于余额
                            ToastUtils.showToast(CodeUtils.getIdString(R.string.string_hintInputRightAccountWithdrawError));
                        } else {
                            isSetPayPwd();
                        }
                    }
                }

                break;
        }
    }

    /**
     * 判断是否设置支付密码
     */
    public void isSetPayPwd() {
        JsonApiWrapper.getApiService().userInfo(SpUtils.getToken(), SpUtils.getMemberId())
                .compose(new DefaultTransformer<CompanyBaseModel<List<UserInfoModel>>>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel<List<UserInfoModel>>>(this, true) {
                    @Override
                    public void onNext(CompanyBaseModel<List<UserInfoModel>> listCompanyBaseModel) {
                        if (listCompanyBaseModel.code == 200 && listCompanyBaseModel.data != null && listCompanyBaseModel.data.size() > 0) {
                            if (listCompanyBaseModel.data.get(0).pay_pwd == 1) {
                                showDialog();
                            } else {
                                DialogComment mDialogComment = new DialogComment(CodeUtils.getIdString(R.string.dialog_setPayPwd), new DialogComment.DialogCommentInterfaca() {
                                    @Override
                                    public void onConfirmListener() {
                                        SetPayPwdStepOneActivity.open(WithdrawActivity.this, CodeUtils.getIdString(R.string.string_setPayPwd));
                                    }
                                });
                                mDialogComment.show(getSupportFragmentManager(), "TAG");
                            }
                        } else {
                            super.onNext(listCompanyBaseModel);
                        }
                    }
                });
    }

    /**
     * 弹出输入支付密码的弹窗
     */
    public void showDialog() {
        DialogInoutPayPwd mPayPwd = new DialogInoutPayPwd(viewDelegate.getAmount(), mBalance, new DialogInoutPayPwd.OnInputFinishListener() {
            @Override
            public void onFinish(String result) {
                withdraw(result);
            }
        });
        mPayPwd.show(getSupportFragmentManager(), "WithdrawActivity");
    }

    /**
     * 提现
     */
    public void withdraw(String pwd) {
        String pwdMd5 = DataUtils.md5(DataUtils.md5(pwd + CodeConstant.SET_PAY_PWD_MD5));
        JsonApiWrapper.getApiService().userWithdraw(SpUtils.getToken(), SpUtils.getMemberId(), viewDelegate.getAccount(), viewDelegate.getAmount(), pwdMd5)
                .compose(new DefaultTransformer<CompanyBaseModel>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel>(this, true) {
                    @Override
                    public void onNext(CompanyBaseModel companyBaseModel) {
                        if (companyBaseModel.code == NetCode.HTTP_200) {
                            viewDelegate.setSuccess();
                        } else {
                            super.onNext(companyBaseModel);
                        }
                    }
                });
    }
}
